import sys

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from form import Ui_MainWindow
from child import Ui_Form


# 接收者
class MyChild(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

    # ② 定义接受需要的槽函数
    def receive(self, string):
        self.ui.label.setText(string)


# 发送者
class MyMainWindow(QMainWindow):  # 主入口
    completed = pyqtSignal(str)  # ① 定义信号

    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.pushButton.clicked.connect(self.open_windows)

    def open_windows(self):
        # 获取用户输入
        string = self.ui.lineEdit.text()

        # 实例化子窗口
        self.child = MyChild()
        self.child.show()

        # ③ 连接《自定义信号》和《槽函数》
        self.completed.connect(self.child.receive)

        # ④ 发送信号
        self.completed.emit(string)


def main():
    app = QApplication(sys.argv)
    window = MyMainWindow()
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
